package bsts.view.customer;

/**
 * not in use. Replaced by AcctManagePanel
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import bsts.application.BSTSApplication;
import bsts.model.Account;
import bsts.model.AccountStatus;
import bsts.model.Customer;

public class DeactivateAcctPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JFrame container;
	private JFrame containerParent;
	private BSTSApplication manager;

	private JPanel pnlMain;
	private JPanel pnlBasicInfo;
	private JPanel pnlCustomer;
	private JPanel pnlAccount;
	private JPanel pnlAccountBtns;
	private JPanel pnlAccountDisp;
	private JPanel pnlButtons;

	private JLabel lblSurname;
	private JLabel lblFirstname;
	private JLabel lblSecondname;
	private JLabel lblNric;
	private JLabel lblAddress;
	private JLabel lblTelephoneNo;
	private JLabel lblSalutation;

	private JLabel lblSurname_data;
	private JLabel lblFirstname_data;
	private JLabel lblSecondname_data;
	private JLabel lblNric_data;
	private JLabel lblAddress_data;
	private JLabel lblTelephoneNo_data;
	private JLabel lblSalutation_data;

	private JTable tblAccounts;
	private JScrollPane scrAccounts;

	private JButton btnActivate;
	private JButton btnDeactivate;
	private JButton btnSave;
	private JButton btnCancel;

	private ActionListener actlis_btnActivate;
	private ActionListener actlis_btnDeactivate;
	private ActionListener actlis_btnSave;
	private ActionListener actlis_btnCancel;

	private Customer customer;
	private ArrayList<Account> accounts = new ArrayList<Account>();

	public DeactivateAcctPanel(JFrame containerParent, JFrame container,
			BSTSApplication manager, Customer customer) {
		this.containerParent = containerParent;
		this.container = container;
		this.manager = manager;
		this.customer = customer;
		accounts = customer.getAccounts();

		pnlMain = new JPanel();
		pnlBasicInfo = new JPanel();
		pnlCustomer = new JPanel();
		pnlAccount = new JPanel();
		pnlAccountBtns = new JPanel();
		pnlAccountDisp = new JPanel();
		pnlButtons = new JPanel();

		pnlMain.setLayout(new BorderLayout());
		pnlCustomer.setLayout(new GridLayout(7, 2));
		pnlAccount.setLayout(new BoxLayout(pnlAccount, BoxLayout.Y_AXIS));
		pnlAccountBtns.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pnlAccountDisp.setLayout(new BorderLayout());
		pnlButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));

		pnlBasicInfo.setBorder(BorderFactory.createTitledBorder("Basic Info"));
		pnlAccount.setBorder(BorderFactory.createTitledBorder("Interest"));

		lblSurname = new JLabel("Surname:");
		lblFirstname = new JLabel("First name:");
		lblSecondname = new JLabel("Second name:");
		lblNric = new JLabel("NRIC:");
		lblAddress = new JLabel("Address:");
		lblTelephoneNo = new JLabel("Telephone No.:");
		lblSalutation = new JLabel("Salutation:");

		lblSurname_data = new JLabel(customer.getSurname());
		lblFirstname_data = new JLabel(customer.getFirstname());
		lblSecondname_data = new JLabel(customer.getSecondname());
		lblNric_data = new JLabel(customer.getNric());
		lblAddress_data = new JLabel(customer.getAddress());
		lblTelephoneNo_data = new JLabel(customer.getTelephoneNo());
		lblSalutation_data = new JLabel(customer.getSalutation());

		lblSurname_data.setPreferredSize(new Dimension(150, lblSurname_data
				.getHeight()));

		tblAccounts = new JTable();
		scrAccounts = new JScrollPane();

		initTbl();
		scrAccounts
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrAccounts.add(tblAccounts, null);
		scrAccounts.setViewportView(tblAccounts);
		scrAccounts.setPreferredSize(new Dimension(500, 150));

		btnActivate = new JButton("Activate");
		btnDeactivate = new JButton("Deactivate");
		btnSave = new JButton("Save");
		btnCancel = new JButton("Cancel");

		pnlCustomer.add(lblSurname);
		pnlCustomer.add(lblSurname_data);
		pnlCustomer.add(lblFirstname);
		pnlCustomer.add(lblFirstname_data);
		pnlCustomer.add(lblSecondname);
		pnlCustomer.add(lblSecondname_data);
		pnlCustomer.add(lblNric);
		pnlCustomer.add(lblNric_data);
		pnlCustomer.add(lblAddress);
		pnlCustomer.add(lblAddress_data);
		pnlCustomer.add(lblTelephoneNo);
		pnlCustomer.add(lblTelephoneNo_data);
		pnlCustomer.add(lblSalutation);
		pnlCustomer.add(lblSalutation_data);

		pnlBasicInfo.add(pnlCustomer);

		pnlAccountBtns.add(btnActivate);
		pnlAccountBtns.add(btnDeactivate);

		pnlAccountDisp.add(scrAccounts, BorderLayout.CENTER);
		pnlAccount.add(pnlAccountBtns);
		pnlAccount.add(pnlAccountDisp);

		pnlButtons.add(btnSave);
		pnlButtons.add(btnCancel);

		pnlMain.add(pnlBasicInfo, BorderLayout.WEST);
		pnlMain.add(pnlAccount, BorderLayout.EAST);
		pnlMain.add(pnlButtons, BorderLayout.SOUTH);

		this.add(pnlMain);

		initActionListener();
	}

	public void refreshTbl() {
		tblAccounts.revalidate();
		tblAccounts.repaint();
	}

	private void initTbl() {
		final String[] strHeaded = { "Account No.", "Activated Date",
				"Deactivated Date", "Account Status" };

		// Create a model of the data.
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			public int getColumnCount() {
				return strHeaded.length;
			}

			public int getRowCount() {
				return accounts.size();
			}

			public Object getValueAt(int row, int col) {
				Account account = accounts.get(row);

				if (col == 0) {
					return account.getAccountNo();
				} else if (col == 1) {
					return account.getStrActivated_Date();
				} else if (col == 2) {
					return account.getStrDeactivated_Date();
				} else if (col == 3) {
					return account.getAccountStatus().toString();
				} else {
					return null;
				}
			}

			public String getColumnName(int column) {
				return strHeaded[column];
			}

			public Class<?> getColumnClass(int col) {
				return getValueAt(0, col).getClass();
			}
		};

		tblAccounts = new JTable(dataModel);
		tblAccounts.getSelectionModel();
		tblAccounts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableCellRenderer right = new DefaultTableCellRenderer();
		right.setHorizontalAlignment(DefaultTableCellRenderer.TRAILING);

		tblAccounts.setDefaultRenderer(Object.class, right);

		int[] intWidth = { 150, 125, 125, 100 };

		for (int i = 0; i < intWidth.length; i++) {
			TableColumn tblCol = tblAccounts.getColumn(strHeaded[i]);
			tblCol.setMaxWidth(intWidth[i]);
		}
	}

	private void initActionListener() {
		actlis_btnActivate = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Account account = accounts.get(tblAccounts.getSelectedRow());
				account.setAccountStatus(AccountStatus.Activated);
				customer.putAccount(account);
				refreshTbl();
			}
		};

		actlis_btnDeactivate = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Account account = accounts.get(tblAccounts.getSelectedRow());
				account.setAccountStatus(AccountStatus.Deactivated);
				customer.putAccount(account);
				refreshTbl();
			}
		};

		actlis_btnSave = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.getCustomerManager().updateCustomer(customer);
				JOptionPane.showMessageDialog(container,
						"Customer account is updated.");
				manager.getViewManager().goBackParent(containerParent,
						container);
			}
		};

		actlis_btnCancel = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.getViewManager().goBackParent(containerParent,
						container);
			}
		};

		btnActivate.addActionListener(actlis_btnActivate);
		btnDeactivate.addActionListener(actlis_btnDeactivate);
		btnSave.addActionListener(actlis_btnSave);
		btnCancel.addActionListener(actlis_btnCancel);
	}
}
